<template>
  <a-spin :spinning="confirmLoading">
    <a-form :form="form">
      <a-row>
        <!-- 表单数据 -->
        <ZcglPutAssetInfo ref="putInfo"></ZcglPutAssetInfo>
        <a-col :span="24" style="margin-top: 15px">
          <a-form-item label="标题" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
            <a-input v-if="!disabled" v-decorator="['title', validatorRules.title]" rows="4" placeholder="请输入标题" />
            <span v-else>{{ data.title }}</span>
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="申请说明" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
            <a-textarea v-if="!disabled" v-decorator="['processDescription', validatorRules.processDescription]"
                        rows="4" placeholder="请输入申请说明" />
            <span v-else>{{ data.processDescription }}</span>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <BaseForm v-if="!disabled" ref="baseForma"></BaseForm>
    <a-form-item v-if="!disabled"
                 :wrapperCol="{ span: 24 }"
                 style="text-align: center">
      <a-button type="primary" :disabled="disabled || btndisabled" @click="handleSubmit">保存</a-button>
      <a-button style="margin-left: 8px" :disabled="disabled" @click="close">取消</a-button>
    </a-form-item>
    <a-form-item v-if="task"
                 :wrapperCol="{ span: 24 }"
                 style="text-align: center">
      <a-button type="primary" @click="passTask">通过</a-button>
      <a-button style="margin-left: 8px" @click="backTask">驳回</a-button>
    </a-form-item>
  </a-spin>
</template>

<script>

import pick from 'lodash.pick'
import '@/assets/less/TableExpand.less'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import { ActivitiFormMixin } from '@/views/activiti/mixins/activitiFormMixin'
import JEditableTable from '@comp/jeecg/JEditableTable'
import { AssetSubMixin } from '@api/assetSubMixin'
import BaseForm from './BaseForm'
import { VALIDATE_NO_PASSED, validateFormAndTables } from '@/utils/JEditableTableUtil'
import { httpAction } from '@api/manage'
import ZcglPutAssetInfo from './ZcglPutAssetInfo'

export default {
  name: 'ZcglPutAssetAdd',
  mixins: [ActivitiFormMixin, AssetSubMixin],
  components: {
    BaseForm,
    ZcglPutAssetInfo,
    JEditableTable,
    JDate,
    JDictSelectTag,
    JSearchSelectTag
  },
  data () {
    return {
      // form
      form: this.$form.createForm(this),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      validatorRules: {
        title: {
          rules: [
            { required: true, message: '请输入标题!' }
          ]
        },
        processDescription: {
          rules: [
            { required: true, message: '请输入申请说明!' }
          ]
        }
      }
    }
  },
  created () {
    this.init()
  },
  methods: {
    setValue () {
      this.$refs.putInfo.disableSubmit = this.disabled
      this.$refs.putInfo.showModal(this.data.dataMap)
      this.data.title = this.processData.title

      if (!this.disabled) {
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.data, 'processDescription', 'title'))
          if (this.processData.procDefId) {
            let keys = []
            keys.push(this.processData.procDefId)
            this.$refs.baseForma.selectedRowKeys = keys
          }
          this.$refs.baseForma.queryParam = { status: 1, routeName: this.processData.routeName }
          this.$refs.baseForma.loadData(1)
        })
      }
    },
    // handler
    handleSubmit (e) {

      let that = this
      /** 触发表单验证 */
      this.$refs.putInfo.getAllTable().then(tables => {
        /** 一次性验证主表和所有的次表 */
        return validateFormAndTables(this.$refs.putInfo.form, tables)
      }).then(allValues => {
        if (typeof this.$refs.putInfo.classifyIntoFormData !== 'function') {
          throw this.$refs.putInfo.throwNotFunction('classifyIntoFormData')
        }
        let a = this.$refs.putInfo.classifyIntoFormData(allValues)

        this.form.validateFields((err, values) => {
          if (!err) {
            if (this.$refs.baseForma.selectedRowKeys.length != 1) {
              this.$message.warning('请选择流程图')
              return
            }

            this.confirmLoading = true
            httpAction(this.$refs.putInfo.url.edit, a, 'put').then((res) => {
              if (res.success) {
                let formData = values
                formData.data = a
                formData.procDefId = this.$refs.baseForma.selectedRowKeys[0]
                that.handleSubmitVa(formData)
              } else {
                this.$message.warning(res.message)
              }
            }).finally(() => {
              this.confirmLoading = false
            })
          }
        })

        // 发起请求
        // return this.request(formData)
      }).catch(e => {
        if (e.error === VALIDATE_NO_PASSED) {
          // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
          this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
        } else {
          console.error(e)
        }
      })
      // this.form.validateFields((err, values) => {
      //   if (!err) {
      //     if (this.$refs.baseForma.selectedRowKeys.length != 1) {
      //       this.$message.warning('请选择流程图')
      //       return
      //     }
      //     values.filedNames = 'processDescription'
      //     values.procDefId = this.$refs.baseForma.selectedRowKeys[0]
      //     this.handleSubmitVa(values)
      //   }
      // })
    }
  }
}
</script>

